//used templates from
//http://bootsnipp.com/snipps/a-row-of-buttons
if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
}
var __beforeStartApp = function(__app, callBack){
     //templates are already compiled!
    //var comp = _.template(templateLoader.templates['tpl-notebook'])
    //note $(document.body).html(comp({id:'test', title: 'title', description: 'test', 'dateCreate': 'test', tags: ''}));
    var __templatesUrl = 'templates/';
    var __templateNames = [
        'tpl-form-field',
        'tpl-item',
        'tpl-toolbar'
    ];
    var that = this;
    var loadTemplates = function(templateNames, callBack){
        if(templateNames.length == 0){
            callBack.call(this);
        }else{
            var templateName = templateNames.shift();
            return templateLoader.loadRemoteTemplate(templateName, __templatesUrl+templateName+'.html', function(){
                return loadTemplates(templateNames, callBack);
            });
        }
    };
    var templateNames = _.clone(__templateNames);
    loadTemplates(templateNames, function(){
        $.get('assets/lists.json').done(function(res){
            __app.setLists(res);
            callBack();
        });
    });
};
var __start = function(){
    //log the SQL queries?
    Backbone.WebSQLStore.debug = true;
    //set the app name, namespace for all sections and pages
    __app.setAppName('m4wd');
    //set the optional start function
    __app.setBeforeStartFunction(__beforeStartApp);
    //must have a corresponding model and collection
    __app.setDbNames(['cars', 'races', 'race_entries', 'race_entry_heats']);
    __app.setDbIns('tester', '', 'tester description', (1024 * 1024));
    __app.setRouter(m4wd_backbone_router);
    __app.setSectionsContainer('div.container');
    __app.ignite();
    
    //workaround for the modal
    $('#main-menu-modal a').on('click', function(ev){
        $('#main-menu-modal button[data-dismiss="modal"]').click();
    });
    
    //go back to top
    $('a#base-back-to-top').on('click', function(ev){
        ev.preventDefault();
        $('html, body').animate({ scrollTop: 0 }, 'fast'); 
    });
}
$(document).ready(__start);


